/**
 * @date 10:59:07 AM
 * SubjectLessonAPIServiceImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.entity.EducationHomeWork;
import com.qs.education.bean.entity.EducationHomeWorkQuestion;
import com.qs.education.bean.entity.EducationQuestion;
import com.qs.education.bean.entity.EducationSubjectLesson;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.service.BaseService;
import com.qs.education.service.ISubjectLessonAPIService;

/**
 * @class SubjectLessonAPIServiceImpl
 * @author Warren
 * @description
 * @date Nov 20, 2018
 */
@Service
public class SubjectLessonAPIServiceImpl extends BaseService implements ISubjectLessonAPIService {

	/**
	 * @author Warren
	 * @return ResponseListBean<EducationSubjectLesson>
	 * @description 获取所有
	 * @date Nov 20, 2018
	 */
	@Override
	public ResponseListBean<EducationSubjectLesson> getALL() {
		ResponseListBean<EducationSubjectLesson> responseListBean = new ResponseListBean<EducationSubjectLesson>();
		List<EducationSubjectLesson> list = iEducationSubjectLessonDao.getAll(EducationSubjectLesson.class);
		if (list != null && !list.isEmpty()) {
			responseListBean.setList(list);
		}
		return responseListBean;
	}

	/**
	 * @Description 根据课时选择 题目以及数量
	 * @author Leon
	 * @date Nov 20, 2018
	 * @param
	 * @return ResponseListBean<EducationSubjectLesson>
	 */
	@Override
	public ResponseListBean<EducationSubjectLesson> selectTopicAndNumber(List<String> lessonIds) {
		ResponseListBean<EducationSubjectLesson> lessonList = new ResponseListBean<EducationSubjectLesson>();
		EducationSubjectLesson lesson = null;
		for (String x : lessonIds) {
			lesson = new EducationSubjectLesson();
			lesson = iEducationSubjectLessonDao.get(EducationSubjectLesson.class, Long.parseLong(x));
			lesson.setQuestionList(null);
			lesson.setUnit(null);
			Integer number = iEducationSubjectLessonDao.selectQuestionCount(Long.parseLong(x));
			lesson.setHomeworkList(null);
			lesson.setNumebr(number);
			lessonList.getList().add(lesson);
		}
		return lessonList;
	}

	/**
	 * @Description 根据选择的课时 题目数量 场景 以及 题目难度进行 智能组题
	 * @author Leon
	 * @date Nov 21, 2018
	 * @param params：[ {lessonId:1,number:2}, {lessonId:2,number:3} ] scenes: 场景
	 *        level ： 等级
	 * @return ResponseListBean<EducationQuestion>
	 */
	@Override
	public ResponseListBean<EducationQuestion> getQuestionListByLesson(String params, String scenes, String level) {
		List<LinkedHashMap> list = JSONObject.parseArray(params, LinkedHashMap.class);
		List<String> lessonIds = new ArrayList<String>();
		List<String> numbers = new ArrayList<String>();
		for (LinkedHashMap map : list) {
			Iterator iter = map.entrySet().iterator();
			int num = 0;
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				Object key = entry.getKey();
				Object value = entry.getValue();
				if (num % 2 == 0)
					lessonIds.add(value.toString());
				if (num % 2 == 1)
					numbers.add(value.toString());
				System.out.println("key: " + key.toString() + " value : " + value.toString());
				num++;
			}
		}
		ResponseListBean<EducationQuestion> questionList = new ResponseListBean<EducationQuestion>();
		List<EducationQuestion> allQuestion;
		allQuestion = iEducationSubjectLessonDao.getQuestionListByLesson(lessonIds, numbers, scenes, level);
		questionList.setList(allQuestion);
		return questionList;
	}

}
